/**
 * 子弹
 * Created by zhd on 2017/6/23.
 */
var BulletSprite = cc.Sprite.extend({
    _tag: null, // 子弹颜色标识
    _name: null, // 子弹图片名字

    _turret_range: 0,// 子弹和炮台的距离
    _radian: 0,// 子弹相对于屏幕的弧度
    _add_speed: 0.1, // 加速度
    _speed: 20, // 子弹发射速度

    /**
     * ctor
     * @param index GC.turrets 子弹下标
     */
    ctor: function (index) {
        this._super();
        this.loadInit(index);
    },

    /**
     * 初始化数据
     * @param index
     */
    loadInit: function (index) {
        this._tag = GC.turrets[index].tag;
        this._name = GC.turrets[index].name;

        this.setSpriteFrame(this._name + "1.png");
        this.loadAnimation();
    },

    /**
     * 加载动画
     */
    loadAnimation: function () {
        var frames = [];
        for (var i = 1; i < 10; i++) {
            var name = this._name + i + ".png";

            var frame = cc.spriteFrameCache.getSpriteFrame(name);
            frames.unshift(frame);
        }
        for (var i = 1; i < 5; i++) {
            for (var j = 0; j < 10; j++) {
                if (i == 4 && j > 8) {
                    continue;
                }

                var name = this._name + i + j + ".png";
                var frame = cc.spriteFrameCache.getSpriteFrame(name);
                frames.unshift(frame);
            }
        }
        var animation = new cc.Animation(frames, 0.03);
        animation.setRestoreOriginalFrame(true);
        var animate = new cc.animate(animation);
        this.runAction(animate.repeatForever());
    },

    /**
     * 子弹发射
     */
    radiate: function () {
        this._turret_range = gameLayer._turretSprite.height / 2 - this.height / 3;
        this._radian = this.rotation * (Math.PI / 180);

        this.scheduleUpdate();
    },

    /**
     * 加速度
     * @returns {number}
     */
    getAddSpeed: function () {
        if (this._old_add_speed == undefined) {
            this._old_add_speed = this._add_speed;
        }
        this._add_speed = this._add_speed + this._old_add_speed;
        return this._add_speed;
    },

    /**
     * 圆碰撞检测
     * @param c1
     * @param c2
     * @returns {boolean}
     */
    collisionBetweenCircles: function (c1, c2) {
        var distanceOfCenter = Math.sqrt(Math.pow((c1.x - c2.x), 2) +
            Math.pow((c1.y - c2.y), 2));
        var distanceOfRadius = c1.r + c2.r;
        if (distanceOfCenter <= distanceOfRadius) {
            return true;
        } else {
            return false;
        }
    },

    /**
     * 子弹发射调度器
     */
    update: function () {
        this._turret_range = this._turret_range + this._speed + this.getAddSpeed();
        var x = gameLayer._turretSprite.x + this._turret_range * Math.sin(this._radian);
        var y = gameLayer._turretSprite.y + this._turret_range * Math.cos(this._radian);

        // 超出屏幕清理
        if (this.x < -this.width ||
            this.y < -this.height ||
            this.x > g_winWidth + this.width ||
            this.y > g_winHeight + this.height) {
            gameLayer._turretSprite._isRadiated = true;
            this.removeAllChildren();
            gameLayer.removeChild(this);
        } else {
            // 碰撞检测
            var ballsSprite = gameLayer._ballManageLayer._ballsSprite;
            for (var i = 0; i < ballsSprite.length; i++) {
                if (this.collisionBetweenCircles(GC.circle(ballsSprite[i].x, ballsSprite[i].y, ballsSprite[i].width / 2),
                        GC.circle(this.x, this.y, this.width / 2))) {

                    // 计算旋转角度，判断像哪边插入
                    var radian = Math.atan2(this.x - ballsSprite[i].x, this.y - ballsSprite[i].y);
                    var rotation = 180 / Math.PI * radian - ballsSprite[i].rotation;
                    rotation = rotation < 0 ? 360 + rotation : rotation;
                    rotation = rotation < 0 ? 360 + rotation : rotation;
                    rotation = rotation < 0 ? 360 + rotation : rotation;

                    if (rotation <= 90 || ( rotation >= 270 && rotation < 360)) {
                        gameLayer._ballManageLayer.insertBeforeBall(i, this._tag, this.getPosition());
                    } else {
                        gameLayer._ballManageLayer.insertAfterBall(i, this._tag, this.getPosition());
                    }
                    this.unscheduleAllCallbacks();
                    gameLayer.removeChild(this);
                    return;
                }
            }
        }

        this.setPosition(x, y);
    },

});